package com.wang.controller;

import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
public class ParameterController {
    @GetMapping("/parameter1/{id}/{username}")
    public Map<String, Object> parameter1(@PathVariable("id") int id, @PathVariable("username") String username){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", id);
        map.put("username", username);
        System.out.println(map);
        return map;
    }
    /*
    * restful风格传递参数时，可以用一个map集合接收所有参数
    * 注意：参数前还需要写@PathVariable注解*/
    @GetMapping("/parameter2/{id}/{username}")
    public Map<String, Object> parameter2(@PathVariable Map<String, Object> map){
        System.out.println(map);
        return map;
    }
    @GetMapping("/parameter3")
    public Map<String, Object> parameter3(@RequestHeader("Host") String host,
            @PathVariable Map<String, Object> map){
        System.out.println(host);
        System.out.println("-----------------------");
        System.out.println(map);
        return map;
    }
    @GetMapping("/parameter4/{id}/{username}")
    public Map<String, Object> parameter4(@PathVariable("id") int id,
                                          @PathVariable("username") String username,
                                          @RequestParam("age") int age,
                                          @RequestParam Map<String, Object> map){
        System.out.println(id);
        System.out.println(username);
        System.out.println(age);
        System.out.println("-------------------------------");
        System.out.println(map);
        return map;
    }
    /*
    * RequestBody:获取请求体的内容
    * 注意：只有post1请求才有请求体*/
    @PostMapping("/parameter5")
    public String parameter5(@RequestBody String requestBody){
        System.out.println(requestBody);
        return requestBody;
    }
}
